﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace PSDServer
{
    partial class PSDSvrService : ServiceBase
    {
        PSDServerContext context = null;

        public PSDSvrService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (Settings.Read())
            {
                // TODO: 在此处添加代码以启动服务。
                context = new PSDServerContext("sql", true);
                string username = System.Configuration.ConfigurationManager.AppSettings["UserName"];
                string password = System.Configuration.ConfigurationManager.AppSettings["PWD"];
                var result = context.Login(Settings.ApiServerAddress, username, password);
                if (result.success)
                {
                    context.Init();
                    context.Start();
                }
                else
                {
                    context.Logger.Error("服务启动失败：" + result.message);
                }
            }
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            if (context != null)
            {
                context.Stop();
            }
        }
    }
}
